package cn.ztuo.bitrade.controller.earningsTreasure;

import cn.ztuo.bitrade.annotation.AccessLog;
import cn.ztuo.bitrade.constant.AdminModule;
import cn.ztuo.bitrade.constant.SysConstant;
import cn.ztuo.bitrade.controller.BaseController;
import cn.ztuo.bitrade.entity.Admin;
import cn.ztuo.bitrade.entity.EarningsBuyRecord;
import cn.ztuo.bitrade.entity.EarningsTreasure;
import cn.ztuo.bitrade.service.EarningsBuyRecordService;
import cn.ztuo.bitrade.service.EarningsTreasureService;
import cn.ztuo.bitrade.util.MessageResult;
import cn.ztuo.bitrade.vo.EarningsBuyRecordVO;
import cn.ztuo.bitrade.vo.EarningsTreasureVO;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

/**
 * @author: Xiazt
 * @date: 2020-04-08 16:49
 * <p>
 * 收益宝 控制器
 */
@RestController
@RequestMapping("earningsTreasure")
@Slf4j
public class EarningsTreasureController extends BaseController {

    @Autowired
    private EarningsTreasureService earningsTreasureService;

    @Autowired
    private EarningsBuyRecordService earningsBuyRecordService;


    /**
     * 获取收益宝列表
     *
     * @param earningsTreasureVO
     * @return
     */
    @RequiresPermissions("earningsTreasure:page-query")
    @RequestMapping(value = "page-query")
    @AccessLog(module = AdminModule.EARNINGSTREASURE, operation = "分页查找收益宝")
    public MessageResult getByPage(@SessionAttribute(SysConstant.SESSION_ADMIN) Admin admin,EarningsTreasureVO earningsTreasureVO) {
        log.info("分页查询收益宝" + JSONObject.toJSONString(earningsTreasureVO));
        try {
            Page<EarningsTreasure> page = earningsTreasureService.getByPage(earningsTreasureVO);
            return success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return error("服务器异常！");
        }
    }

    /**
     * 添加 or 修改
     *
     * @param admin
     * @param earningsTreasure
     * @return
     */
    @RequiresPermissions("earningsTreasure:save")
    @RequestMapping(value = "saveOrUpdateEarningsTreasure")
    @AccessLog(module = AdminModule.EARNINGSTREASURE, operation = "添加或修改收益宝")
    public MessageResult saveEarningsTreasure(@SessionAttribute(SysConstant.SESSION_ADMIN) Admin admin, EarningsTreasure earningsTreasure) {
        return earningsTreasureService.saveEarningsTreasure(earningsTreasure);
    }

    /**
     * 根据ID删除收益宝
     *
     * @return
     */
    @RequiresPermissions("earningsTreasure:delete")
    @RequestMapping(value = "delete")
    @AccessLog(module = AdminModule.EARNINGSTREASURE, operation = "删除收益宝")
    public MessageResult delete(@SessionAttribute(SysConstant.SESSION_ADMIN) Admin admin,Long id) {
        EarningsTreasure earningsTreasure = earningsTreasureService.getById(id);
        if (earningsTreasure == null) return error(400, "ID不存在");
        if (earningsTreasure.getStatus() == 0) {
            earningsTreasure.setStatus(3);
            earningsTreasureService.inster(earningsTreasure);
            return success("删除成功");
        } else {
            return error(400, "只能删除已结束收益宝");
        }
    }

    /**
     * 获取收益宝购买记录列表
     *
     * @param earningsBuyRecordVO
     * @return
     */
    @RequiresPermissions("earningsBuyRecord:page-query")
    @RequestMapping(value = "earningsBuyRecord-page-query")
    @AccessLog(module = AdminModule.EARNINGBUYRECORD, operation = "分页查找收益宝购买记录")
    public MessageResult getEarningsBuyRecordByPage(EarningsBuyRecordVO earningsBuyRecordVO){
        log.info("分页查询收益宝购买记录" + JSONObject.toJSONString(earningsBuyRecordVO));
        try {
            Page<EarningsBuyRecord> page = earningsBuyRecordService.getByPage(earningsBuyRecordVO);
            return success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return error("服务器异常！");
        }
    }


}